package cn.vconinfo.database.aspect;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;

/**
 * @author vconinfo
 * @createdOn 2018/4/30
 * @description:
 */
public class DynamicDataSourceContextHolder {
    private static Logger logger = LoggerFactory.getLogger(DynamicDataSourceContextHolder.class);

    private static final ThreadLocal<String> contextHolder  = new ThreadLocal<String>();
    public static List<String> dataSourceIds = new ArrayList<String>();
    public static void setDataSourceType(String dataSourceType) {
        logger.info("切换至{}数据源", dataSourceType);
        contextHolder.set(dataSourceType);
    }

    public static String getDataSourceType() {
        return contextHolder.get();
    }

    public static void clearDataSourceType() {
        contextHolder.remove();
    }

    /**
     * 判断指定DataSrouce当前是否存在
     *
     * @param dataSourceId
     * @return
     */
    public static boolean containsDataSource(String dataSourceId){
        return dataSourceIds.contains(dataSourceId);
    }
}
